/**
 * Created by IntelliJ IDEA.
 * User: Tomek
 * Date: 24.06.12
 * Time: 15:54
 * To change this template use File | Settings | File Templates.
 */
package AS3 {
import AS3.constants.combo.Income;

import mx.core.FlexGlobals;

public class CountryPostInitializer {

    private var economicGrowth:Number = 2;

    private var mainApp:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;

    public function CountryPostInitializer() {
    }

    public function postInit(country:Country):Country {
        country = adjustConsumption(country);
        country = adjustEnergy(country);
        country = adjustEmissions(country);
        country = adjustPopulation(country);

        country.consumption2000 = country.consumption2000 / country.economyEfficiency;
        country.carPer1000 = calcCarPer1000(country);

        country.name = FossilLocales.lang == 'pl' ? country.namePl : country.nameEn;

        return country;
    }

    private function calcCarPer1000(country:Country):Number {
        var result:Number = 1.0;
        if (country.consumption2000 <= 1000) {
            result = 0.8 * Math.pow(country.consumption2000 / 200, 2.5);
        }
        if ((country.consumption2000 > 1000) && (country.consumption2000 <= 3500)) {
            result = 40 + 25 * (country.consumption2000 - 1000) / 200;
        }
        if (country.consumption2000 > 3500) {
            result = -453 + 300 * Math.log((country.consumption2000 - 2000) / 3) * Math.LOG10E;
        }
        return result;
    }

    private function adjustPopulation(country:Country):Country {
        var i:int;
        for (i = 6; i < 51; i++){
            country.setPopulationForYear(country.getPopulationForYear(5) * Math.pow(country.getPopulationForYear(5) / country.getPopulationForYear(4), i - 5), i);
        }

        return country;
    }

    private function adjustEmissions(country:Country):Country {
        if (country.emissionPerCapitaEq < 0.001) {
            country.emissionPerCapitaEq = country.emissionPerCapita;
            if (country.consumption2000 > 5000) {
                country.emissionPerCapitaEq = country.emissionPerCapita + (country.consumption2000 - 5000) * 0.0002;
            }
        }

        return country;
    }

    private function adjustConsumption(country:Country):Country {
        var richCoefficient:Number = calculateRichCoefficient();
        var growthCoefficient:Number = calculateCoefficientsForYear(0,country,economicGrowth);
        country.consumption2000 = country.consumption2000 * growthCoefficient;

        return country;
    }

    private function adjustEnergy(country:Country):Country {
        country.coalEnergyPercentage = country.coalEnergyPercentage / 100;
        country.oilEnergyPercentage = country.oilEnergyPercentage / 100;
        country.gasEnergyPercentage = country.gasEnergyPercentage / 100;
        country.nuclearEnergy = country.nuclearEnergy / 100;
        country.hydroEnergy = country.hydroEnergy / 100;

        country.renewableEnergy = 1.0 - country.coalEnergyPercentage - country.oilEnergyPercentage - country.gasEnergyPercentage - country.nuclearEnergy - country.hydroEnergy;

        country.coalEnergyOriginal = country.coalEnergyPercentage;
        country.hydroEnergyOriginal = country.hydroEnergy;
        country.oilEnergyOriginal = country.oilEnergyPercentage;
        country.gasEnergyOriginal = country.gasEnergyPercentage;
        country.nuclearEnergyOriginal = country.nuclearEnergy;
        country.renewableEnergyOriginal = country.renewableEnergy;


        return country;

    }

    public function calculateCoefficientsForYear(year:int, chosenCountry:Country, economicGrowth:Number):Number {
        var growthCoefficient:Number;

        var iterationYear:int;
        var iterationPop:int;

        var yearDate:Date = new Date();
        var perCapitaGDPGrowth:Number;
        var perCapitaPopulationGrowth:Number;
        var pop1:Number;
        var pop2:Number;
        var adjustedEcGrowth:Number = 0.75;

        if (economicGrowth == 1) {
            adjustedEcGrowth = 1.00;
        }
        if (economicGrowth == 2) {
            adjustedEcGrowth = 1.25;
        }
        if (economicGrowth == 3) {
            adjustedEcGrowth = 1.50;
        }
        if (economicGrowth == 4) {
            adjustedEcGrowth = 1.75;
        }

        if (year == 0) {
            year = yearDate.getFullYear();
        }
        if (year < 2004) {
            year = 2004;
        }
        for (iterationYear = 2004; iterationYear <= year; iterationYear++) {
            perCapitaGDPGrowth = 1 + (0.4 + 21000 / (chosenCountry.consumption2000 + 5000)) / 100 * adjustedEcGrowth;
            if (chosenCountry.nameEn == 'China') {
                perCapitaGDPGrowth = 1 + (perCapitaGDPGrowth - 1) * (200 / (iterationYear - 1900));

            }
            iterationPop = (iterationYear - 2000) / 10;
            pop1 = chosenCountry.getPopulationForYear(iterationPop) + (chosenCountry.getPopulationForYear(iterationPop + 1) - chosenCountry.getPopulationForYear(iterationPop)) / 10 * (iterationYear - (2000 + 10 * iterationPop));
            iterationPop = (iterationYear + 1 - 2000) / 10;
            pop2 = chosenCountry.getPopulationForYear(iterationPop) + (chosenCountry.getPopulationForYear(iterationPop + 1) - chosenCountry.getPopulationForYear(iterationPop)) / 10 * (iterationYear + 1 - (2000 + 10 * iterationPop));
            perCapitaPopulationGrowth = pop2 / pop1;
            if (perCapitaPopulationGrowth < 1)
            {
                perCapitaPopulationGrowth = 1;
            }
            growthCoefficient = perCapitaGDPGrowth / perCapitaPopulationGrowth;
        }

        return growthCoefficient;
    }

    public function calculateRichCoefficient():Number {
        var richCoefficient:Number = 1.0;
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_10_PER_CENT) {
            richCoefficient = 0.05;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_20_PER_CENT) {
            richCoefficient = 0.20;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_50_PER_CENT) {
            richCoefficient = 0.50;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_80_PER_CENT) {
            richCoefficient = 0.80;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE) {
            richCoefficient = 1.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_120_PER_CENT) {
            richCoefficient = 1.20;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_150_PER_CENT) {
            richCoefficient = 1.50;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_200_PER_CENT) {
            richCoefficient = 2.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_300_PER_CENT) {
            richCoefficient = 3.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_500_PER_CENT) {
            richCoefficient = 5.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_10) {
            richCoefficient = 10.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_20) {
            richCoefficient = 20.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_50) {
            richCoefficient = 50.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_100) {
            richCoefficient = 100.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_200) {
            richCoefficient = 200.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_500) {
            richCoefficient = 500.00;
        }
        if (mainApp.konsumpcja_controls.income == Income.AVERAGE_1000) {
            richCoefficient = 1000.00;
        }

        return richCoefficient;
// Przeliczenie konsumpcji wyrażonej w tonach CO2 w funkcji bogactwa
//24000	3,5; 18000	3; 12000	2,5; 6000	2; 3000	1,5; 1500	0,75; 1000	0,4; 500	0,1; 300	0,025; 150	0,01
    }
}
}
